package com.jdaw.datastructure.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PolandNotation {
    public static void main(String[] args) {

        //1+((2+3)×4)-5 => "1 2 3 + 4 × + 5 –"
        String expression="1+((2+3)*4)-5";
        List<String> resList=toInfixExpressionList(expression);
        List<String> ls=parseSuffixExpressionList(resList);
        System.out.println(ls);


//        String suffixExpression="3 4 + 5 * 6 -";
//        List<String> rpnList=getListString(suffixExpression);
//        System.out.println(calculate(rpnList));
    }
    //将表达式放入arraylist中处理
    public static List<String> getListString(String suffixExpression){
        String[] split=suffixExpression.split(" ");
        List<String> list=new ArrayList<>();
        for(String ele:split){
            list.add(ele);
        }
        return list;
    }

    public static List<String> toInfixExpressionList(String s){
        List<String> ls=new ArrayList<String>();
        int i=0;
        String str;
        char c;
        do{
            if((c=s.charAt(i))<48||(c=s.charAt(i))>57){//比较asicii码
                ls.add(c+"");
                i++;
            }else{
                str="";
                //拼接多位数字
                while(i<s.length()&&(c=s.charAt(i))>=48&&(c=s.charAt(i))<=57){
                    str+=c;
                    i++;
                }
                ls.add(str);
            }

        }while(i<s.length());
        return ls;
    }

    public static List<String> parseSuffixExpressionList(List<String> ls){
        Stack<String> s1=new Stack<String>();
        //Stack<String> s2=new Stack<String>();可以用list代替
        List<String> s2=new ArrayList<String>();
        for(String item:ls){
            if(item.matches("\\d+")){
                s2.add(item);
            }else if(item.equals("(")){
                s1.push(item);
            }else if(item.equals(")")){
                while(!s1.peek().equals("(")){
                    s2.add(s1.pop());
                }
                s1.pop();//将左括号也抛了
            }else{
                while(s1.size()!=0&&Operation.getValue(s1.peek())>=Operation.getValue(item)){
                    s2.add(s1.pop());
                }
                s1.push(item);//遇到操作符比较s1栈顶的优先级，若栈空或为（，直接入栈；若优先级高于栈顶元素则也直接入；
            }
        }
        while(s1.size()!=0){
            s2.add(s1.pop());
        }
        return s2;
    }

    public static int calculate(List<String> ls){
        Stack<String> sStack=new Stack<String>();
        for(String item:ls){
            //使用正则表达式匹配
            if(item.matches("\\d+")){// 匹配的是多位数
               sStack.push(item);
            }else{
                int num2=Integer.parseInt(sStack.pop());
                int num1=Integer.parseInt(sStack.pop());
                int res=0;
                if(item.equals("+")){
                    res=num1+num2;
                }else if(item.equals("-")){
                    res=num1-num2;
                }else if(item.equals("*")){
                    res=num2*num1;
                }else if(item.equals("/")){
                    res=num2/num1;
                }else{
                    throw new RuntimeException("运算符错误");
                }
                sStack.push(res+"");
            }

        }
        return Integer.parseInt(sStack.pop());
    }
}

//operation返回优先级比较
class Operation {
    private static int ADD = 1;
    private static int SUB = 1;
    private static int MUL = 2;
    private static int DIV = 2;

    //写一个方法，返回对应的优先级数字
    public static int getValue(String operation) {
        int result = 0;
        switch (operation) {
            case "+":
                result = ADD;
                break;
            case "-":
                result = SUB;
                break;
            case "*":
                result = MUL;
                break;
            case "/":
                result = DIV;
                break;
            default:
                System.out.println("不存在该运算符" + operation);
                break;
        }
        return result;
    }

}